package cj.web.portal.controllers;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import cj.web.admin.constants.Constants;
import cj.web.portal.service.MemberVoService;
import cj.web.portal.service.VideoUcVoService;

/**
 * 用户中心
 * @author Administrator
 *
 */
@Path("ucVo")
public class MemberVoController extends BaseVoController{
	@Autowired
	private MemberVoService memberService;
	@Autowired
	private VideoUcVoService videoUcService;
	
	/**
	 * 观看历史page
	 * @param model
	 * @param request
	 * @return
	 */
	@Get("watchHistory")
	public String watchHistory(Model model, HttpServletRequest request){
		String memberId = getCookieValueByName(request, "memberId");
		
		if("".equals(memberId)){
			model.add("watchHistorylis", null);
			model.add("memberId", "");
			model.add("member", null);
		}else{
			List<Map> watchHistorylis = videoUcService.getGroupTime(Integer.parseInt(memberId), 0, 3);
			for(Map time:watchHistorylis){
				List<Map> lis = videoUcService.getWatchHistoryByCreateAtlis(time.get("createAt").toString(),Integer.parseInt(memberId));
				time.put("lis", lis);
			}
			
			model.add("watchHistorylis", watchHistorylis);
			model.add("memberId", getCookieValueByName(request, "memberId"));
			List<Map> memberlis = memberService.getMemberById(Integer.parseInt(memberId));
			model.add("member", memberlis.get(0));
		}
		
		return "/views/portal/user_center.vm";
	}
	
	/**
	 * 观看历史方法
	 * @param start
	 * @param length
	 * @param request
	 * @return
	 */
//	@Post("getWatchHistoryList")
//	public String getWatchHistoryList(@Param("start") int start,@Param("length") int length, HttpServletRequest request) {
//	
//		JSONObject jo = new JSONObject();
//		String memberId = getCookieValueByName(request, "memberId");
//		
//		Map rep = videoUcService.queryWatchHistorylis(Integer.parseInt(memberId), start*length, length);
//		List<Map> objs = (List<Map>) rep.get("watchHistorylis");
//		int size = (int) rep.get("size");
//		
//		jo.put("rows", objs);
//		jo.put("total", size);
//		return "@"+jo.toString();
//	}
	
	
	/**
	 * 消费历史page
	 * @param model
	 * @param request
	 * @return
	 */
	@Get("payHistory")
	public String payHistory(Model model, HttpServletRequest request){
		String memberId = getCookieValueByName(request, "memberId");
		
		if("".equals(memberId)){
			model.add("memberId", "");
			model.add("member", null);
		}else{
			model.add("memberId", getCookieValueByName(request, "memberId"));
			List<Map> memberlis = memberService.getMemberById(Integer.parseInt(memberId));
			model.add("member", memberlis.get(0));
		}
		
		return "/views/portal/user_property.vm";
	}
	
	/**
	 * 消费历史方法
	 * @param start
	 * @param length
	 * @param request
	 * @return
	 */
	@Get("getPayHistoryList")
	public String getPayHistoryList(@Param("start") int start,@Param("length") int length, HttpServletRequest request) {
	
		JSONObject jo = new JSONObject();
		String memberId = getCookieValueByName(request, "memberId");
		
		if("".equals(memberId)){
			jo.put("returnFlag", Constants.returnObj.returnFlag_true);
			jo.put("list", null);
			jo.put("totalCount", 0);
		}else{
			Map rep = videoUcService.queryPayHistorylis(Integer.parseInt(memberId), start*length, length);
			List<Map> objs = (List<Map>) rep.get("payHistorylis");
			
			int size = (int) rep.get("size");
			jo.put("returnFlag", Constants.returnObj.returnFlag_true);
			jo.put("list", objs);
			jo.put("totalCount", size);
		}
		
		return "@"+jo.toString();
	}
	
	/**
	 * 关于我们
	 * @param model
	 * @param request
	 * @return
	 */
	@Get("aboutAs")
	public String aboutAs(Model model, HttpServletRequest request){
		String memberId = getCookieValueByName(request, "memberId");
		
		if("".equals(memberId)){
			model.add("memberId", "");
			model.add("member", null);
		}else{
			model.add("memberId", getCookieValueByName(request, "memberId"));
			List<Map> memberlis = memberService.getMemberById(Integer.parseInt(memberId));
			model.add("member", memberlis.get(0));
		}
		return "/views/portal/about_us.vm";
	}
	
	//---------------------------------------
	/**
	 * 修改密码界面
	 * @param model
	 * @param request
	 * @return
	 */
//	@Get("upPwdPage")
//	public String upPwdPage(Model model, HttpServletRequest request){
//		if(isLogin(request)){
//			String phone = getCookieValueByName(request, "phone");
//			String memberId = getCookieValueByName(request, "memberId");
//			model.add("memberId", memberId);
//			model.add("phone", phone);
//		}
//		return "/views/uc/wx/upPwd.vm";
//	}
	
	/**
	 * 修改密码界面-PC
	 * @param model
	 * @param request
	 * @return
	 */
//	@Get("upPcPwdPage")
//	public String upPcPwdPage(Model model, HttpServletRequest request){
//		if(isLogin(request)){
//			String phone = getCookieValueByName(request, "phone");
//			String memberId = getCookieValueByName(request, "memberId");
//			model.add("memberId", memberId);
//			model.add("phone", phone);
//		}
//		return "/views/uc/upPcPwd.vm";
//	}
	
	/**
	 * 修改密码
	 * @param phone
	 * @param newPwd
	 * @param oldPwd
	 * @param response
	 * @return
	 * @throws Exception
	 */
//	@Post("upPwd")
//	public String upPwd(@Param("memberId")int memberId, @Param("newPwd")String newPwd, 
//			@Param("oldPwd")String oldPwd, HttpServletResponse response) throws Exception{
//		JSONObject jo = new JSONObject();
//		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
//		List<Map> uc = memberService.getMemberById(memberId);
//		if(uc.size() == 0){
//			//不存在
//			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
//			jo.put("returnCode", Constants.returnObj.returnCode.code_2);
//		}else if(uc.size()>0){
//			//存在
//			String pwd = uc.get(0).get("pwd").toString();//pwd
//			
//			if(pwd.equals(MD5Security.md5(oldPwd))){
//				memberService.updateMemberId(memberId, MD5Security.md5(newPwd));
//			}else{
//				jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
//			}
//		}
//		
//		return "@" + jo.toString();
//	}
	
	/**
	 * 用户完善信息界面
	 * @param model
	 * @param request
	 * @return
	 */
//	@Get("upInforPage")
//	public String ucInforPage(Model model, HttpServletRequest request){
////		model.add("sexObj",EnumSet.allOf(SexEnum.class));
//		if(isLogin(request)){
//			String memberId = getCookieValueByName(request, "memberId");
//			model.add("memberId", memberId);
//			List<Map> uc = memberService.getMemberById(Integer.parseInt(memberId));
//			if(uc.size()>0){
//				model.add("obj", uc.get(0));
//			}else{
//				model.add("obj", null);
//			}
//		}
//		return "/views/uc/wx/updateInfor.vm";
//	}
	
	/**
	 * 用户完善信息界面
	 * @param model
	 * @param request
	 * @return
	 */
//	@Get("upPcInforPage")
//	public String upPcInforPage(Model model, HttpServletRequest request){
////		model.add("sexObj",EnumSet.allOf(SexEnum.class));
//		if(isLogin(request)){
//			String phone = getCookieValueByName(request, "phone");
//			String memberId = getCookieValueByName(request, "memberId");
//			model.add("memberId", memberId);
//			model.add("phone", phone);
//			List<Map> uc = memberService.getMemberById(Integer.parseInt(memberId));
//			if(uc.size()>0){
//				model.add("obj", uc.get(0));
//			}else{
//				model.add("obj", null);
//			}
//		}
//		return "/views/uc/updatePcInfor.vm";
//	}
	
	/**
	 * 用户信息修改
	 * @param model
	 * @param memberId
	 * @param name
	 * @param sex
	 * @param age
	 * @param career
	 * @param request
	 * @return
	 */
//	@Post("upInfor")
//	public String ucInforPage(Model model,@Param("memberId")int memberId,@Param("name")String name,@Param("sex")String sex,
//			@Param("age")String age,@Param("career")String career, HttpServletRequest request,HttpServletResponse response){
//		JSONObject jo = new JSONObject();
//		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
//		List<Map> uc = memberService.getMemberById(memberId);
//		if(uc.size() == 0){
//			//不存在
//			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
//		}else if(uc.size()>0){
//			//存在
//			setCookie(response, "sex", sex);//性别
//			memberService.updateMemberId(memberId, name, age, sex, career);
//		}
//		return "@" + jo.toString();
//	}
}